package com.xijia.wuyouwallet.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.xijia.wuyouwallet.MyApp;
import com.xijia.wuyouwallet.R;
import com.xijia.wuyouwallet.api.MyCallBack;
import com.xijia.wuyouwallet.base.BaseActivity;
import com.xijia.wuyouwallet.utils.CharCheckUtil;
import com.xijia.wuyouwallet.utils.Constants;
import com.xijia.wuyouwallet.utils.MyLogger;
import com.xijia.wuyouwallet.utils.UrlConfig;
import com.xijia.wuyouwallet.utils.XUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/18 0018.
 */

public class AplipayBingdingActivity extends BaseActivity implements View.OnClickListener{

    private ImageView aplipayBingdng_back;
    private EditText aplipayBingdng_phone_et,aplipayBingdng_name_et,aplipayBingdng_code_et;
    private TextView aplipayBingdng_tv,aplipayBingdng_sure;

    private int countSeconds = 60;// 倒数秒数

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_aplipay_binding);
        initView();
    }

    private Handler hanlder = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    if (countSeconds > 0) {
                        --countSeconds;
                        aplipayBingdng_tv.setText(countSeconds + "s");
                        hanlder.sendEmptyMessageDelayed(0, 1000);
                    } else {
                        countSeconds = 60;
                        aplipayBingdng_tv.setText("重新获取");
                        aplipayBingdng_tv.setClickable(true);
                    }
                    break;
            }
        }
    };
    // 开始倒计时
    private void startCountBack() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                aplipayBingdng_tv.setText(countSeconds + "s");
                hanlder.sendEmptyMessage(0);
                aplipayBingdng_tv.setClickable(false);
            }
        });
    }

    private void initView(){
        aplipayBingdng_back = (ImageView) findViewById(R.id.aplipayBingdng_back);
        aplipayBingdng_back.setOnClickListener(this);
        aplipayBingdng_phone_et = (EditText) findViewById(R.id.aplipayBingdng_phone_et);
        aplipayBingdng_name_et = (EditText) findViewById(R.id.aplipayBingdng_name_et);
        aplipayBingdng_code_et = (EditText) findViewById(R.id.aplipayBingdng_code_et);
        aplipayBingdng_tv = (TextView) findViewById(R.id.aplipayBingdng_tv);
        aplipayBingdng_tv.setOnClickListener(this);
        aplipayBingdng_sure = (TextView) findViewById(R.id.aplipayBingdng_sure);
        aplipayBingdng_sure.setOnClickListener(this);

    }
    private void getCode(){
        String phone = aplipayBingdng_phone_et.getText().toString().trim();
        if (phone.isEmpty()){
            showToast("请输入手机号");
            return;
        }
        if (!CharCheckUtil.isPhoneNum(phone)){
            showToast("手机号格式错误");
            return;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("phone",phone);
        XUtil.Post(UrlConfig.GETCODE_URL,map,new MyCallBack<String>(){

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                //MyLogger.wLog().e("获取验证码result="+result);
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (jsonObject.getInteger("code")==200){
                    startCountBack();
                }
                showToast(jsonObject.getString("msg"));
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                MyLogger.wLog().e("获取验证码失败");
            }
        });
    }
    private void bingdingAplipay(){
        final String acount = aplipayBingdng_phone_et.getText().toString().trim();
        if (acount.isEmpty()){
            showToast("请输入您的账号");
            return;
        }
        final String name = aplipayBingdng_name_et.getText().toString().trim();
        if (name.isEmpty()){
            showToast("请输入您的真实姓名");
            return;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("token",MyApp.userData.getData().getToken());
        map.put("user",MyApp.userData.getData().getUser_id());
        map.put("pay_type_code","alipay");
        map.put("pay_type_name","支付宝");
        map.put("account",acount);
        map.put("other_info",name);
        XUtil.Post(UrlConfig.BINDING_WX_APLIPAY_URL,map,new MyCallBack<String>(){

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                MyLogger.wLog().e("绑定支付宝result="+result);
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (jsonObject.getInteger("code")==200){
                    MyApp.userData.getData().getAccount_info().getAlipay().setState(1);
                    MyApp.userData.getData().getAccount_info().getAlipay().setAccount(acount);
                    MyApp.userData.getData().getAccount_info().getAlipay().setInfo(name);
                    AplipayBingdingActivity.this.setResult(Constants.RESULT_DATA);
                    AplipayBingdingActivity.this.finish();
                }
                showToast(jsonObject.getString("msg"));
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                MyLogger.wLog().e("绑定支付宝失败"+ex.toString());
            }
        });
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.aplipayBingdng_back:
                finish();
                break;
            case R.id.aplipayBingdng_sure:
                bingdingAplipay();
                break;
            case R.id.aplipayBingdng_tv:
                getCode();
                break;
        }
    }
}
